package com.tanhua.sso.controller;

import com.tanhua.sso.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: MyCenterController
 * @Author:王晨晗
 * @Description: 我的中心 修改头像
 * @Date: 2022/01/07/15:08
 */
@RestController
@RequestMapping("users")
public class MyCenterController {
    @Autowired
    private UserInfoController userInfoController;
    @Autowired
    private UsersService usersService;

    /**
     * @description 头像上传
     * @param file  头像文件
     * @param token 用户令牌
     * @return
     */
    @PostMapping("header")
    public ResponseEntity<Object> saveLogo(@RequestParam("headPhoto") MultipartFile file
                                        , @RequestHeader("Authorization") String token) {
        //调用sso系统原有的上传头像的方法 并返回结果
        return userInfoController.head(token , file);
    }

    /**
     * @description 更换手机号-发送验证码
     * @param token 用户令牌;
     * @return
     */
    @PostMapping("phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(@RequestHeader("Authorization") String token){
        //调用发送验证码方法
        usersService.sendVerificationCode(token);
        //返回结果
        return ResponseEntity.ok(null);
    }

    /**
     * @description 校验验证码
     * @param token 用户令牌
     * @param param 验证码
     * @return
     */
    @PostMapping("phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestHeader("Authorization") String token,
                                                @RequestBody Map<String , String> param){
        //获取参数 用户填写的验证码
        String code = param.get("verificationCode");
        //调用校验验证码的方法 , 获取结果集
        Map<String , Object> result = usersService.checkVerificationCode(token , code);
        //返回响应数据
        return ResponseEntity.ok(result);
    }

    /**
     * @description 更新用户手机号-保存手机号
     * @param token 用户令牌
     * @param param 手机号
     * @return
     */
    @PostMapping("phone")
    public ResponseEntity saveNewPhone(@RequestHeader("Authorization") String token,
                                       @RequestBody Map<String , String> param){
        //获取到新手机号
        String newPhone = param.get("phone");
        //保存新手机号
        usersService.saveNewPhone(token , newPhone);
        //返回响应结果
        return ResponseEntity.ok(null);
    }
}
